package com.xusheng.cli.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.MyServiceImpl;
import com.xusheng.cli.entity.backAgentModule.BackAgent;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.frontUserModule.VipConfig;
import com.xusheng.cli.entity.vipRecordModule.VipRecord;
import com.xusheng.cli.mapper.FrontUserMapper;
import com.xusheng.cli.req.FrontUserQueryReq;
import com.xusheng.cli.service.*;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FrontUserServiceImpl extends MyServiceImpl<FrontUserMapper, FrontUser> implements FrontUserService {

    @Resource
    private ProxyHostService proxyHostService;
    @Resource
    private ProxyHostTwoService proxyHostTwoService;
    @Resource
    private ProxyHostThreeService proxyHostThreeService;
    @Resource
    private BackAgentService backAgentService;
    @Resource
    private VipConfigService vipConfigService;
    @Resource
    private VipRecordService vipRecordService;

    @Override
    public MyPage<FrontUser> userList(FrontUserQueryReq req) {
        JoinLambdaWrapper<FrontUser> wrapper = new JoinLambdaWrapper<>(FrontUser.class);
        LocalDateTime start = null, end = null;
        if (req.getStartTime() != null) {
            start = LocalDateTime.of(req.getStartTime(), LocalTime.of(0, 0, 0));
        }
        if (req.getEndTime() != null) {
            end = LocalDateTime.of(req.getEndTime(), LocalTime.of(23, 59, 59));
        }
        Integer proxyLevel = req.getProxyLevel();
        List<Long> proxyList = new ArrayList<>();
        if (proxyLevel != null) {
            if (proxyLevel == 1) {
                proxyList = proxyHostService.list().stream().map(proxyHost -> proxyHost.getProxyId()).collect(Collectors.toList());
            } else if (proxyLevel == 2) {
                proxyList = proxyHostTwoService.list().stream().map(proxyHost -> proxyHost.getProxyId()).collect(Collectors.toList());
            } else if (proxyLevel == 3) {
                proxyList = proxyHostThreeService.list().stream().map(proxyHost -> proxyHost.getProxyId()).collect(Collectors.toList());
            }
            if (proxyList.size() == 0) {
                proxyList.add(0L);
            }
        }

        wrapper.gt(start != null, FrontUser::getCreateTime, start)
                .lt(end != null, FrontUser::getCreateTime, end)
                .isNull(req.getProxyLevel() != null && req.getProxyLevel() == 0, FrontUser::getProxyId)
                .in(req.getProxyLevel() != null && req.getProxyLevel() > 0 && proxyList.size() > 0, FrontUser::getProxyId, proxyList)
                .like(StrUtil.isNotBlank(req.getAccount()), FrontUser::getAccount, req.getAccount())
                .like(StrUtil.isNotBlank(req.getHost()), FrontUser::getHost, req.getHost())
                .orderByDesc(FrontUser::getCreateTime)
                .leftJoin(VipConfig.class, VipConfig::getId, FrontUser::getVipId)
                .end();
        MyPage<FrontUser> frontUserMyPage = baseMapper.joinSelectPage(req.getPage(), wrapper, FrontUser.class);
        for (FrontUser frontUser : frontUserMyPage.getRecords()) {
            if (frontUser.getProxyId() != null) {
                BackAgent backAgent = backAgentService.getById(frontUser.getProxyId());
                if (backAgent != null) {
                    frontUser.setProxyName(backAgent.getName());
                }
            }
            if (frontUser.getVipId() != null) {
                frontUser.setVipName(vipConfigService.getById(frontUser.getVipId()).getName());
            }
            frontUser.setVipCount(Math.toIntExact(vipRecordService.lambdaQuery().eq(VipRecord::getUserId, frontUser.getId()).count()));
        }
        return frontUserMyPage;
    }

    /**
     * @param inviteCode
     * @return
     */
    @Override
    public List<FrontUser> getMyPromotionList(String inviteCode) {
        return baseMapper.getMyPromotionList(inviteCode);
    }

    /**
     * @param proxyId
     */
    @Override
    public void removeProxyId(Long proxyId) {
        baseMapper.removeProxyId(proxyId);
    }
}

